package com.qiche.service.impl;

import com.qiche.aspect.Result;
import com.qiche.mapper.SubscribeProductMapper;
import com.qiche.model.Subscribe;
import com.qiche.dto.SubscribeProductDto;
import com.qiche.enums.ResultEnum;
import com.qiche.enums.SubscribeStatusEnum;
import com.qiche.model.Product;
import com.qiche.model.SubscribeProduct;
import com.qiche.service.SubscribeProductService;
import com.qiche.service.SubscribeService;

import tk.mybatis.mapper.entity.Example;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chendezhi
 * @date 2018/3/5 10:17
 */
@Service
public class SubscribeProductServiceImpl extends BaseService<SubscribeProduct> implements SubscribeProductService {
	@Autowired
	private SubscribeProductMapper subscribeProductMapper;
	@Autowired
	private SubscribeService subscribeService;

	private SubscribeProductDto createSubcribeProductDto(Product product) {
		SubscribeProductDto subscribeProductDto = new SubscribeProductDto();
		BeanUtils.copyProperties(product, subscribeProductDto);
		return subscribeProductDto;
	}

	@Override
	public List<Product> selectProductBySubscribeId(Long subId) {
		return subscribeProductMapper.selectProductBySubscribeId(subId);
	}

	@Override
	public int insertBatch(List<SubscribeProduct> subscribeProducts) {
		return subscribeProductMapper.insertList(subscribeProducts);
	}


    @Override
    public List<SubscribeProductDto> getProductsBySubscribe(Long subId) {
        List<Product> products = this.selectProductBySubscribeId(subId);
        return products.stream()
                .map(this::createSubcribeProductDto)
                .collect(Collectors.toList());
    }
    @Override
    public Result delProduct(SubscribeProduct subscribeProduct) {
        Subscribe subscribe = subscribeService.selectByKey(subscribeProduct.getSubscribeId());
        Byte subscribeStatus = subscribe.getSubscribeStatus();
        if (!subscribeStatus.equals(SubscribeStatusEnum.OPEN_SUBSCRIBE.getStatus())) {
            //不是开单状态 不容许修改

            return  Result.createByResultEnum(ResultEnum.DEL_SUBSCRIBEPRODUCT_ERROR);
        }
        Example example = new Example(SubscribeProduct.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("subscribeId",subscribeProduct.getSubscribeId());
        List<SubscribeProduct> allSubscribeProducts = subscribeProductMapper.selectByExample(example);
        if (allSubscribeProducts.size()==1){
            return Result.createByResultEnum(ResultEnum.LAST_PRODUCT_OF_SUBSCRIBE_ERROR);
        }
        criteria.andEqualTo("productId",subscribeProduct.getProductId());
        List<SubscribeProduct> subscribeProducts = subscribeProductMapper.selectByExample(example);
        if (subscribeProducts.size()==0){
            //删除失败
            return  Result.createByResultEnum(ResultEnum.FAILURE);
        }
        int delete = subscribeProductMapper.delete(subscribeProducts.get(0));
        if (delete>0){
            //删除成功
            return  Result.createByResultEnum(ResultEnum.SUCCESS);
        }else {
            //删除失败
            return  Result.createByResultEnum(ResultEnum.FAILURE);
        }
    }
    @Override
    public Result addProduct(SubscribeProduct subscribeProduct){
        Subscribe subscribe = subscribeService.selectByKey(subscribeProduct.getSubscribeId());
        Byte subscribeStatus = subscribe.getSubscribeStatus();
        if (!subscribeStatus.equals(SubscribeStatusEnum.OPEN_SUBSCRIBE.getStatus())) {
            //不是开单状态 不容许修改
            return  Result.createByResultEnum(ResultEnum.DEL_SUBSCRIBEPRODUCT_ERROR);
        }
        int insert = subscribeProductMapper.insert(subscribeProduct);
        if (insert>0){
            return  Result.createByResultEnum(ResultEnum.SUCCESS);
        }else {
            return  Result.createByResultEnum(ResultEnum.FAILURE);
        }
    }




	@Override
	public int deleteBySubscribeId(Long subId) {
		Example example = new Example(Subscribe.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("subscribeId", subId);
		return mapper.deleteByExample(example);
	}

    @Override
    public List<SubscribeProduct> selectBySubscribeId(Long subId) {
        Example example = new Example(SubscribeProduct.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("subscribeId", subId);
        return mapper.selectByExample(example);
    }



}
